(function( $ ) {
/**
	form element to object
*/
$.fn.serializeObject = function()
{
    var o = {};
    var a = this.serializeArray();
    $.each(a, function() {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};


$.fn.tmpFile = function(options) {
	var form = this;
	if (typeof options == 'string') {
		if (options == 'save') {
			if (tmpImgIDs.length) {
				$.post('/cf/call/tmp.save',{'id': tmpImgIDs, 'target': arguments[1]});
			}
		}
		if (options == 'cancel') {
			if (tmpImgIDs.length) {
				$.post('/cf/call/tmp.del',{'id': tmpImgIDs}).fail( function(a,b) { console.log(a,b) } );
			}
		}
	} else {
		tmpImgIDs = [];
		
		form.find('input[type=file]').change(function(e){	
			$('iframe[name=tmp_image]').one('load', function(e){
				var f = $.parseJSON( $(this).contents().find('body').html() );
				options.onAdd(f[1],f[0]);
				tmpImgIDs.push(f[0]);
			});
			form.submit();
		});
	}
}

})( jQuery );